package com.sinoglobal.ningxia.frame;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.RelativeLayout;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.utils.StringUtil;
import com.umeng.socialize.bean.CallbackConfig.ICallbackListener;
import com.umeng.socialize.bean.RequestType;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.bean.StatusCode;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

/**
 * @author lgy
 * @createdate 2015-1-29 下午3:41:25
 * @Description: (用一句话描述该类做什么) 有分享界面需要继承的基类 需要关联
 * social_sdk_library_project这个library
 */
public abstract class ShareAbstractActivity extends BaseActivity {
    // 首先在您的Activity中添加如下成员变量
    protected final UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share", RequestType.SOCIAL);
    private ICallbackListener listener;
    private UMImage umImage;
    private QQShareContent qqShareContent;
    private QZoneShareContent qZoneShareContent;
    private WeiXinShareContent weixinContent;
    private CircleShareContent circleMedia;

    private Dialog shareDialog;// 分享弹出的
    private GridView share; // 分享面板
    private int screenWidth; // 屏幕宽度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        shareInfo();
    }

    /**
     * @param arg       //分享平台
     * @param content   //分享内容
     * @param imagePath //分享图片url
     * @param shareUrl  //分享需要传一个地址,作为点击链接
     * @author lgy
     * @createdate 2015-1-29 下午3:23:10
     * @Description: (用一句话描述该方法做什么) 分享面板打开和设置内容
     */
    public void setShare(SHARE_MEDIA arg, String content, String imagePath, String shareUrl) {
        if (StringUtil.isNullOrEmpty(imagePath)) {
            umImage = new UMImage(ShareAbstractActivity.this, R.drawable.ic_launcher);
        } else {
            umImage = new UMImage(ShareAbstractActivity.this, imagePath);
//            umImage.setThumb(imagePath);
        }
        // 设置分享内容
        mController.setShareContent(content);
        mController.setShareImage(umImage);

        qZoneShareContent.setTargetUrl(shareUrl);
        qZoneShareContent.setTitle(getString(R.string.shareTitle));
        qZoneShareContent.setShareContent(content);
        qZoneShareContent.setShareImage(umImage);
        mController.setShareMedia(qZoneShareContent);

        qqShareContent.setTargetUrl(shareUrl);
        qqShareContent.setTitle(getString(R.string.shareTitle));
        qqShareContent.setShareContent(content);
        qqShareContent.setShareImage(umImage);
        mController.setShareMedia(qqShareContent);

        // 设置微信好友内容
        weixinContent.setTargetUrl(shareUrl);
        weixinContent.setTitle(getString(R.string.shareTitle));
        weixinContent.setShareContent(content);
        weixinContent.setShareImage(umImage);
        mController.setShareMedia(weixinContent);

        // 设置微信朋友圈内容
        circleMedia.setTargetUrl(shareUrl);
        circleMedia.setShareContent(getString(R.string.shareTitle));
        circleMedia.setTitle(content);
        circleMedia.setShareImage(umImage);
        mController.setShareMedia(circleMedia);

        // 参数1为当前Activity对象， 参数2为要分享到的目标平台， 参数3为分享操作回调接口
        mController.postShare(ShareAbstractActivity.this, arg, new SnsPostListener() {

            @Override
            public void onComplete(SHARE_MEDIA arg0, int arg1, SocializeEntity arg2) {
                if (arg1 == StatusCode.ST_CODE_SUCCESSED) {
                    showShortToastMessage(getString(R.string.share_ok));
                } else if (arg1 == 40000) {
                } else {
                    showShortToastMessage(getString(R.string.share_fail));
                }
            }

            @Override
            public void onStart() {
                showShortToastMessage(getResources().getString(R.string.shareing));
            }

        });
    }

    /**
     * @author lgy
     * @createdate 2015-3-25 下午3:54:50
     * @Description: 设置初始化
     */
    private void shareInfo() {
        mController.getConfig().setSsoHandler(new SinaSsoHandler()); // 如果新浪安装客户端,会走新浪客户端
        // 选择平台
        mController.getConfig().removePlatform(SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN, SHARE_MEDIA.SMS, SHARE_MEDIA.TENCENT);
        // 设置分享平台面板中显示的平台
        mController.getConfig().setPlatformOrder(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA, SHARE_MEDIA.QQ,
                SHARE_MEDIA.QZONE);

        mController.getConfig().closeToast();// 关闭土司
        weixinContent = new WeiXinShareContent();
        circleMedia = new CircleShareContent();
        qZoneShareContent = new QZoneShareContent();
        qqShareContent = new QQShareContent();

        // TODO 需要替换新申请的QQ互联分享key
        String qqAppID = getString(R.string.qqAppID);
        String qqAppSecret = getString(R.string.qqAppSecret);
        // 参数1为当前Activity，参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(this, qqAppID, qqAppSecret);
        qqSsoHandler.addToSocialSDK();

        // 参数1为当前Activity，参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(this, qqAppID, qqAppSecret);
        qZoneSsoHandler.addToSocialSDK();

        // TODO 需要替换新申请的QQ互联分享key
        String wxAppID = getString(R.string.w_wxAppID);
        String wxAppSecret = getString(R.string.w_wxAppSecret);
        // 添加微信平台
        UMWXHandler wxHandler = new UMWXHandler(this, wxAppID, wxAppSecret);
        wxHandler.addToSocialSDK();
        // 添加微信朋友圈
        UMWXHandler wxCircleHandler = new UMWXHandler(this, wxAppID, wxAppSecret);
        wxCircleHandler.setToCircle(true);
        wxCircleHandler.addToSocialSDK();
    }


    /**
     * @param content   分享内容
     * @param imagePath 分享图片路径(网络图片)
     * @param path      微信图文分享需要网页url
     * @param type      type true 使用面板分享, false 不弹面板直接分享已授权平台
     * @author lgy
     * @createdate 2014-6-30 上午9:29:08
     * @Description: (用一句话描述该方法做什么)
     */
    public void showPopwindow(boolean type, String content, String imagePath, String path) {
        // 需要加&&判断,设置 点赞等是否一键分享的开关状态
        if (!type) {
            // 一键分享
            setShare(null, content, imagePath, path);
            return;
        }

        screenWidth = FlyApplication.widthPixels;
        shareDialog = new AlertDialog.Builder(ShareAbstractActivity.this, R.style.DialogStyleBottom).create();
        shareDialog.show();

        shareDialog.getWindow().setContentView(R.layout.pop_layout);
        shareDialog.getWindow().setGravity(Gravity.BOTTOM);
        android.view.WindowManager.LayoutParams lp = shareDialog.getWindow().getAttributes();
        lp.width = screenWidth; // 设置宽度
        shareDialog.getWindow().setAttributes(lp);
        shareDialog.getWindow().setWindowAnimations(R.style.sharedialog);
        share = (GridView) shareDialog.findViewById(R.id.gv_share);
        RelativeLayout rlShareDialog = (RelativeLayout) shareDialog.findViewById(R.id.rl_share_dialog);
        rlShareDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shareDialog.dismiss();
            }
        });
        share.setAdapter(new com.sinoglobal.ningxia.adapter.ShareAdapter(ShareAbstractActivity.this));
        setShareOnItemClick(content, imagePath, path);
        shareDialog.show();


    }

    /**
     * @author lgy
     * @createdate 2014-6-26 下午4:44:26
     * @Description: (用一句话描述该方法做什么) 初始化控制面板控件
     */
    public void setShareOnItemClick(final String content, final String imagePath, final String path) {

        share.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                switch (arg2) {
                    case 0:// 微信好友
                        setShare(SHARE_MEDIA.WEIXIN, content, imagePath, path);
                        shareDialog.dismiss();
                        break;
                    case 1:// 微信朋友圈
                        setShare(SHARE_MEDIA.WEIXIN_CIRCLE, content, imagePath, path);
                        shareDialog.dismiss();
                        break;
                    case 2:// 新浪微博
                        setShare(SHARE_MEDIA.SINA, content, imagePath, path);
                        shareDialog.dismiss();
                        break;
                    case 3:// QQ好友
                        setShare(SHARE_MEDIA.QQ, content, imagePath, path);
                        shareDialog.dismiss();
                        break;
                    case 4:// QQ空间
                        setShare(SHARE_MEDIA.QZONE, content, imagePath, path);
                        shareDialog.dismiss();
                        break;
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (listener != null) {
            mController.unregisterListener(listener);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /** 使用SSO授权必须添加如下代码 */
        UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode);
        if (ssoHandler != null) {
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }
}
